package com.xinxin.leetcode.problem566;

import java.util.Arrays;

/**
 * @author ShiXinXin
 * @date 2020-02-07 12:25
 */
class Solution {
    public int[][] matrixReshape(int[][] nums, int r, int c) {
        if (nums.length == 0) {
            return nums;
        }
        if (nums.length * nums[0].length != r * c) {
            return nums;
        }
        int[][] res = new int[r][c];
        int m = 0, n = 0;
        for (int[] num : nums) {
            for (int i : num) {
                res[m][n++] = i;
                if (n == c) {
                    n = 0;
                    m++;
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[][] nums = {{1, 2}, {3, 4}};
        System.out.println(Arrays.deepToString(new Solution().matrixReshape(nums, 1, 4)));
    }
}
